#include "gd32f30x.h"   
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "Delay.h"
#include "event_groups.h"
#include "Connect_ONENET.h"
#include "ui.h"
extern SemaphoreHandle_t Mutex_Handle;
extern EventGroupHandle_t Reconnect_EventGroup_Handle;
extern MYTime time;
uint32_t time_open;
void TIM_Init(void)
{
    rcu_periph_clock_enable(RCU_TIMER3);
    timer_internal_clock_config(TIMER3);

    timer_parameter_struct timer0_initStruct;
    timer0_initStruct.alignedmode       = TIMER_COUNTER_EDGE;
    timer0_initStruct.clockdivision     = TIMER_CKDIV_DIV1;
    timer0_initStruct.counterdirection  = TIMER_COUNTER_UP;
    timer0_initStruct.period            = 9999;
    timer0_initStruct.prescaler         = 11999;
    timer0_initStruct.repetitioncounter = 0;
    timer_init(TIMER3, &timer0_initStruct);

    timer_interrupt_enable(TIMER3, TIMER_INT_UP);

    nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
    nvic_irq_enable(TIMER3_IRQn, 2, 2);

    timer_enable(TIMER3);

/*********pwm_confin***********/

    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_TIMER1);

    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);

    timer_parameter_struct timer1init_struct;
    timer_oc_parameter_struct timer1_ocinit_struct;

    timer_deinit(TIMER1);
    
    timer1init_struct.alignedmode      = TIMER_COUNTER_EDGE;
    timer1init_struct.clockdivision    = TIMER_CKDIV_DIV1;
    timer1init_struct.period           = 99;
    timer1init_struct.prescaler        = 1199;
    timer1init_struct.repetitioncounter = 0;
    timer1init_struct.counterdirection = TIMER_COUNTER_UP;
    timer_init(TIMER1, &timer1init_struct);
    
    //timer3_ocinit_struct.ocnidlestate   = TIMER_OCN_IDLE_STATE_LOW;
    timer1_ocinit_struct.ocidlestate      = TIMER_OC_IDLE_STATE_HIGH;
   // timer3_ocinit_struct.ocnpolarity    = TIMER_OCN_POLARITY_HIGH;
    timer1_ocinit_struct.ocpolarity     = TIMER_OC_POLARITY_HIGH;
    //timer3_ocinit_struct.outputnstate   = TIMER_CCXN_DISABLE;
    timer1_ocinit_struct.outputstate    = TIMER_CCX_ENABLE;

    timer_channel_output_config(TIMER1, TIMER_CH_1, &timer1_ocinit_struct);


    timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 0);
    timer_channel_output_mode_config(TIMER1, TIMER_CH_1,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);


    timer_auto_reload_shadow_enable(TIMER1);

    timer_enable(TIMER1);
}
void TIMER3_IRQHandler(void)
{
    
    if(SET == timer_interrupt_flag_get(TIMER3, TIMER_INT_FLAG_UP))
    {
        time_open++;
        time.sec++;
    }
    timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);
}
void TIM_ReadCount_Task(void *parame)
{
    for(;;)
    {
        // BaseType_t ret = xSemaphoreTake(Mutex_Handle, pdMS_TO_TICKS(60));
		// if(ret == pdTRUE)
		// OLED_ShowNum(2,1,timer_counter_read(TIMER3),5);

		// xSemaphoreGive(Mutex_Handle);
        vTaskDelay(20);
    }

}
void TIM_Task(void *param)
{
    for(;;)
    {
        //每过两分钟就检查一下是否连上了mqtt
        // if((time_open % 120) == 0)
        // {
        //     xEventGroupSetBits(Reconnect_EventGroup_Handle, (EventBits_t)(1 << 3));
        // }
        // BaseType_t ret = xSemaphoreTake(Mutex_Handle, pdMS_TO_TICKS(60));
        // if(ret == pdTRUE)
        // show_mqtt_connecting();        
        // xSemaphoreGive(Mutex_Handle);
        vTaskDelay(20); 
    }
}